package com.fbty.mt.intercept;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.fbty.mt.domain.User;

public class EndWithFilter implements Filter {

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest httpRequest = (HttpServletRequest)request;
		HttpServletResponse httpResponse = (HttpServletResponse)response;

		String uri = httpRequest.getRequestURI();
		String cont = httpRequest.getContextPath();
		if(uri.endsWith("login.jsp")||uri.endsWith("regist.jsp")||uri.endsWith("notify_url.jsp")||uri.endsWith("return_url.jsp")){
			chain.doFilter(httpRequest, httpResponse);
		}else{
			HttpSession session=  httpRequest.getSession();
			User u = (User)session.getAttribute("user");
			if(u==null){
				httpResponse.sendRedirect(cont+"/welcome.do");
			}else{
				if(uri.startsWith(cont+"/admin")){
					Boolean b = (Boolean)session.getAttribute("adminLoginFlag");
					if(b!=null&&b&&u.getState()==2){
						chain.doFilter(httpRequest, httpResponse);
					}else{
						httpResponse.sendRedirect(cont+"/welcome.do");
					}
				}else{
					chain.doFilter(httpRequest, httpResponse);
				}
			}
		}
		
 
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}


}
